<?php

class model_pddOrder extends L {

    /**
     * 描述 ：分页的查询语句拼接
     * 作者 ：Zoro.Zhu
    **/
    public static function searchSql($params = array()){
        $sql = "SELECT
             `pddOrder`, `accountPdd`, `confirmTime`, `data`, `addTime`, `orderState`, `aliOrder`, `aliData`
        FROM
            `pdd_order`
        WHERE 1";
        
        $sql .= !empty($params['search']['pddOrder']) ? " AND `pddOrder` = '{$params['search']['pddOrder']}' " : "";
        $sql .= !empty($params['search']['accountPdd']) ? " AND `accountPdd` = '{$params['search']['accountPdd']}' " : "";
        $sql .= !empty($params['search']['confirmTimeStart']) ? " AND `confirmTime` >= '{$params['search']['confirmTimeStart']}' " : "";
        $sql .= !empty($params['search']['confirmTimeEnd']) ? " AND `confirmTime` <= '{$params['search']['confirmTimeEnd']}' " : "";
        $sql .= !empty($params['search']['addTimeStart']) ? " AND `addTime` >= '{$params['search']['addTimeStart']}' " : "";
        $sql .= !empty($params['search']['addTimeEnd']) ? " AND `addTime` <= '{$params['search']['addTimeEnd']}' " : "";
        $sql .= !empty($params['search']['orderState']) ? " AND `orderState` = '{$params['search']['orderState']}' " : "";
        $sql .= !empty($params['search']['aliOrder']) ? " AND `aliOrder` = '{$params['search']['aliOrder']}' " : "";
        return $sql;
    }

    /**
     * 描述 ：执行修改数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function updateSql($data = array()){
        $sql = "UPDATE `pdd_order` 
        SET 
            `accountPdd` = '{$data['accountPdd']}',
            `confirmTime` = '{$data['confirmTime']}',
            `data` = '{$data['data']}',
            `addTime` = '{$data['addTime']}',
            `orderState` = '{$data['orderState']}',
            `aliOrder` = '{$data['aliOrder']}',
            `aliData` = '{$data['aliData']}'
        WHERE
            `pddOrder` = '{$data['pddOrder']}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：执行插入数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function insertSql($data = array()){
        $sql = "INSERT INTO `pdd_order`(
             `accountPdd`, `confirmTime`, `data`, `addTime`, `orderState`, `aliOrder`, `aliData`
        ) VALUES (
             '{$data['accountPdd']}', '{$data['confirmTime']}', '{$data['data']}', '{$data['addTime']}', '{$data['orderState']}', '{$data['aliOrder']}', '{$data['aliData']}'
        )";
        return L::sql($sql);
    }

    /**
     * 描述：根据主键获取数据
     * 作者：Zoro.Zhu
    **/
    public static function getKeyInfo($key){
        $sql = "SELECT
             `pddOrder`, `accountPdd`, `confirmTime`, `data`, `addTime`, `orderState`, `aliOrder`, `aliData`
        FROM
            `pdd_order`
        WHERE 
            `pddOrder` = '{$key}'";
        return L::sql($sql);
    }

    /**
     * 描述：获取订单状态,分别按状态返回订单
     * 作者：Zoro.Zhu
    **/
    public static function getOrderState($order){
        is_array($order) && $order = join("','", $order);
        $sql = "SELECT
             `orderState`, `pddOrder`
        FROM
            `pdd_order`
        WHERE 
            `pddOrder` IN ('{$order}')";
        $res = L::sql($sql);
        $data = [];
        foreach($res as $row){
            $data[$row['pddOrder']] = $row['orderState'];
        }
        return $data;
    }


    /**
     * 描述 ：根据SKU获取产品ID
     * 作者 ：Zoro.Zhu
     **/
    public static function skuGetId($sku){
        $sql = "SELECT
            `ali_product`.`productId`, `ali_product`.`pddId`
        FROM
            `ali_product_detail`
        LEFT JOIN `ali_product` ON `ali_product_detail`.`productId` = `ali_product`.`productId` 
        WHERE
            `ali_product_detail`.`sku` = '{$sku}'";
        $info = L::sql($sql);
        return !empty($info[0]) ? $info[0] : ['productId' => '', 'pddId' => ''];
    }

}